<?php
/**
 * 模板操作类
 *
 * @author virowin<virowin@gmail.com>
 * @package Lib
 *
 */
require_once(ROOT_PATH."/Lib/Smarty/Smarty.class.php");

class Lib_SmartyTpl extends Smarty
{

    private $tpl_type;  //静态文件的类型
    public static $tpl_js = array();  //js文件数组
    public static $tpl_another_js=array();//另一个js数组
    public static $tpl_css = array(); //css文件数组

    /**
     * 构造函数
     *
     * @param void
     * @return Object Instance of Template_Smarty
     * @access public
     * @author virowin<virowin@gmail.com>
     * @version 1.0
     */

    public function __construct()
    {
    	global $CFG;
    	parent::__construct();
        $this->left_delimiter = "<{";
        $this->right_delimiter = "}>";
        $this->template_dir = $CFG ['tpl_path'];
        $this->compile_dir = $CFG ['tplc_path'];
		$this->error_reporting=E_ALL & ~E_NOTICE;
    }

    /**
     * 输出模版
     *
     * @param string $resource_name
     * @param string $cache_id
     * @param string $compile_id
     * @return void
     * @access public
     * @author virowin<virowin@gmail.com>
     * @version 1.0
     */
    public function show($resource_name,$data = null, $cache_id = null, $compile_id = null)
    {
        $this->get($resource_name,$data, $cache_id, $compile_id,'display');
    }

    /**
     * 获取模版编译后模版内容
     *
     * @param string $resource_name
     * @param string $cache_id
     * @param string $compile_id
     * @param boolean $display
     * @return string $content
     * @access public
     * @author virowin<virowin@gmail.com>
     * @version 1.0
     */
    public function get($resource_name,$data = null, $cache_id = null, $compile_id = null, $display = 'fetch')
    {
    	global $CFG;
    	$this->assign('CFG',$CFG);
        $this->assign('data',$data);
        $this->assign('css_files',self::$tpl_css);
        $this->assign('js_files',self::$tpl_js);
        $this->assign('js_another_files',self::$tpl_another_js);
        $this->tpl_type = "htm";
        return $this->$display($resource_name . "." . $this->tpl_type, $cache_id, $compile_id);
    }

    /**
     * 动态添加js文件
     * @param string $js_file
     * @access public
     * @version 1.0
     */
    public function add_js($js_file)
    {
    	if(!in_array($js_file,self::$tpl_js)){
    		self::$tpl_js[] = $js_file;
    	}
    }
    /**
     * 主要是为了一部分js放在头（add_js），一部分js放在尾部（add_another_js）
     * @param string $js_file
     * @access public
     * @version 1.0
     */
    public function add_another_js($js_file)
    {
    	if(!in_array($js_file,self::$tpl_another_js)){
    		self::$tpl_another_js[] = $js_file;
    	}
    }
    /**
     * 动态添加css文件
     * @param string $css_file
     * @access public
     * @version 1.0
     */
    public function add_css($css_file)
    {
    	if(!in_array($css_file,self::$tpl_css)){
    		self::$tpl_css[] = $css_file;
    	}
    }

}
